package cn.cena.service.impl;

import cn.cena.constant.BaseConstant;
import cn.cena.domain.Login;
import cn.cena.dto.RelationLogin2MealDto;
import cn.cena.exception.GlobalException;
import cn.cena.mapper.LoginMapper;
import cn.cena.service.ILoginService;
import cn.cena.service.IMealService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 登录表 服务实现类
 * </p>
 *
 * @author yu
 * @since 2021-11-22
 */
@Service
@Transactional(rollbackFor = Error.class)
public class LoginServiceImpl extends ServiceImpl<LoginMapper, Login> implements ILoginService {

    @Autowired
    private IMealService mealService;

    @Override
    public Login saveLoginAndMeal(RelationLogin2MealDto login2MealDto) {
        // 判断参数 字段中已判断
        Login login = login2MealDto.getLogin();
        Long mealId = login2MealDto.getMealId();
        // 密码 TODO
        // 保存t_login
        login.setType(BaseConstant.Login.TYPE_SYSTEM);
        login.setEnabled(true); // 启用
        login.setAccountNonExpired(true);
        login.setAccountNonLocked(true);
        login.setCredentialsNonExpired(true);
        insert(login);

        // 保存t_login_meal
        Long loginId = login.getId();
        mealService.saveRelationLogin2Meal(loginId,mealId);
        return login;
    }
}
